<?php
Class Template{
        public static function getButtons($buttons=""){
        	if($buttons!=""){
        		$result = "";
        		foreach ($buttons as $name => $button) {
					$opt 	= isset($button['opt'])?" data-opt='".json_encode($button['opt'])."'":"";
					$url 	= isset($button['url'])?" data-url='".$button['url']."'":"";
					$action	= isset($button['action'])?' data-action="'.$button['action'].'"':"";
					$class 	= " class='but".(isset($button['class'])?$button['class']:"")."'";
					$result.= "<a".$class.$url.$action.$opt.">".$name."</a>";
				}
				return $result;
        	}
			return "";
        }
        public static function parse($template='',$data=''){
        	return self::parse_($template,$data);
        }
        public static function parse_($template='',$data=''){
                if($template!=''&&is_file($template.".html")){
                        $template = file_get_contents($template.".html");
                }else if($template!=''&&is_file("views".DIRSEP.$template.".html")){
                        $template = file_get_contents("views".DIRSEP.$template.".html");
                }
                $arr = array();
                if($data!=''){
                        $arr = $data;
                }
                foreach($arr as $find => $replace){
                        if(is_string($replace))$template = str_replace("{".$find."}", $replace, $template);
                }
                $res = $template;
                preg_match_all("/{include\s([^}]+)}/i" , $res, $out, PREG_PATTERN_ORDER);
                for($i=0;$i<count($out[0]);$i++){
                        $key = $out[0][$i];
                    $arg = $out[1][$i];
                    $sub = "";
                    $f1 = "views/".$arg;
                        if(!is_file($f1.".html")){
                                $f1 = $arg;
                        }
                    if(is_file($f1.".html")){
                        $sub = Template::parse_($f1,$arr);
                        }
                    $res = str_replace($key,$sub,$res);
                }
                $template = $res;
                return $template;
        }
}
?>